package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.OpBrowseHistory;
import cn.com.hhrcw.entity.OpJob;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.mapper.OpBrowseHistoryMapper;
import cn.com.hhrcw.mapper.OpJobMapper;
import cn.com.hhrcw.service.IOpJobService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
    * 职位-职位本位 服务实现类
    * </p>
 *
 * @author jiangdong
 * @since 2020-10-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OpJobServiceImpl extends ServiceImpl<OpJobMapper, OpJob> implements IOpJobService {
    @Autowired
    private OpJobMapper opJobMapper;
    @Autowired
    private OpBrowseHistoryMapper opBrowseHistoryMapper;

    @Override
    public void updateJobCount(OpJob opJob, SysUser sysUser) {
        opJobMapper.updateById(opJob);
        if (null != sysUser){
            OpBrowseHistory history=new OpBrowseHistory();
            history.setDelFlag(false);
            history.setCreateBy(sysUser.getId());
            history.setCreateTime(new Date());
            history.setComId(opJob.getCompanyId());
            String substring = UuidUtils.getUUID().substring(0, 32);
            history.setId(substring);
            history.setJobId(opJob.getId());
            history.setUserId(sysUser.getId());
            opBrowseHistoryMapper.insert(history);
        }

    }

    @Override
    public void updateRecruitmentState(OpJob opJob){
        opJobMapper.updateById(opJob);
    }


    @Override
    public Integer getJobNatureCount(String companyId, String jobNature) {
        QueryWrapper<OpJob> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(OpJob::getCompanyId, companyId)
            .eq(OpJob::getRecruitmentState, 0)
            .eq(OpJob::getDelFlag, false)
            .likeRight(OpJob::getJobNature, jobNature);

        return baseMapper.selectCount(wrapper);
    }

    @Override
    public String getJobName(String jobId) {
        if(StringUtils.isNotBlank(jobId)){
            OpJob byId = this.getById(jobId);
            return byId.getJobName();
        }
        return null;
    }
}
